package HBaseDemo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;

public class HBaseTest {
    //1.获取configuration对象
    static  Configuration conf ;
    static {
        //1.获取configuration对象
        conf = HBaseConfiguration.create();
        //2.往配置文件中设置参数
        conf.set("hbase.zookeeper.quorum", "bigdata211");
        //代理客户端信息
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        //节点信息
        conf.set("zookeeper.znode.parent", "/hbase");
    }
    public static void main(String[] args) throws IOException {
        
       //isTableExit("person");
       //createTable("Test","info","order","other");
       // putRowData("Test","1001","info","name","XR");
       //deleteRows("Test","1001","1002");
        // getAllRows("Test");
       // getRow("Test","1001");
       // getRowQualifier("Test","1001","","");
        deleteTable("Test");
    }


    //获取指定列簇的数据(获取指定列的信息)
    private static void getRowQualifier(String tableName, String rowKey, String cf, String qualifier) throws IOException {
        //创建HTable对象
        HTable hTable = new HTable(conf, tableName);
        //创建Get对象
        Get get = new Get(Bytes.toBytes(rowKey));
        //封装数据到get对象
        //get.addFamily(Bytes.toBytes(cf));
        get.addColumn(Bytes.toBytes(cf),Bytes.toBytes(qualifier));
        //获取数据
        Result result = hTable.get(get);
        for (Cell cell : result.rawCells()) {
            //先打印rowkey
            System.out.println("行键rowkey"+Bytes.toString(CellUtil.cloneRow(cell)));
            //得到列簇
            System.out.println("列簇"+Bytes.toString(CellUtil.cloneFamily(cell)));
            //得到列名
            System.out.println("列名"+Bytes.toString(CellUtil.cloneQualifier(cell)));
            //得到列名值
            System.out.println("列名值"+Bytes.toString(CellUtil.cloneValue(cell)));

        }
        //关闭流
        hTable.close();

    }

    //读取指定一行数据
    private static void getRow(String tableName  ,String rowKey) throws IOException {
        //创建HTable对象
        HTable hTable = new HTable(conf, tableName);
        //创建Get对象
        Get get = new Get(Bytes.toBytes(rowKey)); 
        /*get.getMaxVersions();//显示版本号
        get.setTimeStamp();//显示时间戳*/
        //获取表的数据（使用hTble查询获取数据）
        Result result = hTable.get(get);
        //for循环遍历获取单元数据
        for (Cell cell : result.rawCells()) {
            //先打印rowkey
            System.out.println("行键rowkey"+Bytes.toString(CellUtil.cloneRow(cell)));
            //得到列簇
            System.out.println("列簇"+Bytes.toString(CellUtil.cloneFamily(cell)));
            //得到列名
            System.out.println("列名"+Bytes.toString(CellUtil.cloneQualifier(cell)));
            //得到列名值
            System.out.println("列名值"+Bytes.toString(CellUtil.cloneValue(cell)));
        }

        //关闭流
        hTable.close();

    }

    private static void getAllRows(String tableName) throws IOException {
        //创建HTable对象
        HTable hTable = new HTable(conf, tableName);
        //获取扫描表的对象
        Scan scan = new Scan();
        ResultScanner scanner = hTable.getScanner(scan);
        for (Result result : scanner) {
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                //先打印rowkey
                System.out.println("行键rowkey"+Bytes.toString(CellUtil.cloneRow(cell)));
                //得到列簇
                System.out.println("列簇"+Bytes.toString(CellUtil.cloneFamily(cell)));
                //得到列名
                System.out.println("列名"+Bytes.toString(CellUtil.cloneQualifier(cell)));
                //得到列名值
                System.out.println("列名值"+Bytes.toString(CellUtil.cloneValue(cell)));

            }

        }
        //关闭流
        hTable.close();

    }

    //删除多行数据（DDL）
    private static void deleteRows(String tableName, String... rows) throws IOException {
        //创建HTable对象
        HTable hTable = new HTable(conf, tableName);
        //创建Delete类型的list集合
         ArrayList<Delete> list =new ArrayList<>();
         //封装rowkey到list集合
        for(String row : rows){
            Delete delete = new Delete(Bytes.toBytes(row));
            list.add(delete);

        }
        //删除数据（list里的数据）
        hTable.delete(list);
        System.out.println("删除成功！");
        //关闭资源
        hTable.close();
    }

    //往表里插入数据
    private static void putRowData(String tableName, String rowKey, String columFamily, String column,String value) throws IOException {
        //创建HTable对象
        HTable hTable = new HTable(conf, tableName);
        //向表里插入数据(设置 rowKey获取put对象）
        Put put = new Put(Bytes.toBytes(rowKey));
        //向put中插入数据(设置列簇，列名，列名值)，封装到了put对象中
        put.add(Bytes.toBytes(columFamily),Bytes.toBytes(column),Bytes.toBytes(value));
        //将数据加入表中
        hTable.put(put);
        //关闭资源
        hTable.close();
        System.out.println("插入成功！");

    }
   //判断表是否存在
    private static boolean isTableExit(String tableName) throws IOException {
        //3.获取连接对象
        //连接HBase
        Connection connection = ConnectionFactory.createConnection(conf);
        HBaseAdmin admin =(HBaseAdmin) connection.getAdmin();
        //HBaseAdmin admin1 = new HBaseAdmin(conf);
        System.out.println(admin.tableExists(tableName));
        return admin.tableExists(tableName);
    }
    //创建表
    private static void createTable(String tableName,String ... columFaily) throws IOException {
        HBaseAdmin admin = new HBaseAdmin(conf);
        if(isTableExit("Test")){
            System.out.println("table"+tableName+"alreay exist");

        }else {
            //创建表描述器
            HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
            //创建多列簇
            for(String cf:columFaily){
                descriptor.addFamily(new HColumnDescriptor(cf));
            }
            //创建表
            admin.createTable(descriptor);
            System.out.println("table"+tableName+"表已经创建！");
        }

    }
    private static void deleteTable(String tableName) throws IOException {
        HBaseAdmin admin = new HBaseAdmin(conf);
        //判断表是否存在
        if(isTableExit("tableName")){
            //禁用表
            admin.disableTable(tableName);
            //删除表
            admin.deleteTable(tableName);
            System.out.println("删除成功");
        }else{
            System.out.println(tableName+"表不存在");

        }
    }


}
